<template>
	<view>
		<view class="cu-custom" :style="[{height: customBar + 'px'}]">
			<view class="cu-bar fixed" :style="style" :class="[bgImage != '' ? 'none-bg text-white bg-img' : '', bgColor]">
				<view class="action" @tap="backPage" v-if="isBack">
					<text class="cuIcon-back"></text>
					<slot name="backText"></slot>
				</view>
				<view class="content" :style="[{top: statusBar + 'px'}]">
					<slot name="content"></slot>
				</view>
				<slot name="right"></slot>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				statusBar: this.statusBar,
				customBar: this.dataHeight > 0 ? this.dataHeight : this.customBar
			};
		},
		name: 'cu-custom',
		computed: {
			style() {
				let statusBar = this.statusBar;
				let customBar = this.dataHeight > 0 ? this.dataHeight : this.customBar;
				let bgImage = this.bgImage;
				let style = `height: ${customBar}px; padding-top: ${statusBar}px;`;

				if (this.bgImage) {
					style = `${style} background-image: url(${bgImage});`;
				}

				return style
			}
		},
		props: {
			isFixed: {
				type: Boolean,
				default: false
			},
			bgColor: {
				type: String,
				default: ''
			},
			isBack: {
				type: [Boolean, String],
				default: false
			},
			bgImage: {
				type: String,
				default: ''
			},
			dataHeight: {
				type: Number,
				default: 0
			}
		},
		methods: {
			backPage() {
				if (getCurrentPages().length < 2 && 'undefined' !== typeof __wxConfig) {
					let url = '/' + __wxConfig.pages[0];
					
					return uni.redirectTo({url})
				}
				uni.navigateBack({
					delta: 1
				});
			}
		}
	}
</script>
